// 只针对一张表的操作 - 学生
const Mock = require('mockjs') // 假数据

module.exports = {
    isTableExite: function (sqliteDB) {
        const sql = `
            select name from sqlite_master where type = 'table' and name = 'student'
        `
        return new Promise((resolve, reject) => {
            sqliteDB.queryData(sql, function(result){
                if (result && result.length) {resolve()} else {reject()}
            });
        })
    },
    create: function (sqliteDB) {
        const sql = `
            create table if not exists "student" (
                "id" INTEGER not null unique,
                "name" TEXT not null default 'noname',
                "sex" INTEGER not null default 1,
                "state" INTEGER not null default 1,
                "age" INTEGER,
                "stay" INTEGER,
                "desc" TEXT,
                "good" TEXT,
                "bad" TEXT,
                primary key("id")
            )
        `
        return new Promise((resolve, reject) => {
            sqliteDB.createTable(sql)
            resolve()
        })
    },
    drop: function (sqliteDB) {
        const sql = `
            drop table "student"
        `
        return new Promise((resolve, reject) => {
            sqliteDB.executeSql(sql);
            resolve()
        })
    },
    mockInsert: function (sqliteDB) {
        // 伪造一个数据来插入数据库
        const mockData = Mock.mock([
            '@integer(10000,90000)',
            '@cname',
            '@pick(["1", "2"])',
            '@pick(["1", "2", "3", "4"])',
            '@integer(18, 100)',
            '@integer(18, 100)',
            '@csentence',
            '@csentence',
            '@cparagraph(1, 3)',
            '/^1(5|3|7|8)[0-9]{9}$/',
            '@cword(2, 4)'
        ])
        const sql = `
            insert into student(id, name, sex, state, age, stay, desc, good, bad)
            values(?, ?, ?, ?, ?, ?, ?, ?, ?)
        `
        return new Promise((resolve, reject) => {
            try {
                const result = sqliteDB.insertData(sql, [mockData]) // insertData 是二维数组
                resolve(result)
            } catch (e) {
                reject(e)
            }
        })
    },
    insert: function (sqliteDB, insertData) {
        const sql = `
            insert into student(id, name, sex, state, age, stay, desc, good, bad)
            values(?, ?, ?, ?, ?, ?, ?, ?, ?)
        `
        return new Promise((resolve, reject) => {
            const result = sqliteDB.insertData(sql, insertData) // insertData 是二维数组
            resolve(result)
        })
    },
    update: function (sqliteDB, id, name, sex, state, age, stay, desc, good, bad) {
        const sql = `
            update student
            set name = ${name}, sex = ${sex}, state = ${state}, age = ${age}, desc = ${desc}, good = ${good}, bad = ${bad}
            where id = ${id}
        `
        return new Promise((resolve, reject) => {
            const result = sqliteDB.executeSql(sql);
            resolve(result)
        })
    },
    list: function (sqliteDB) {
        const sql = `
            select * from student
        `
        return new Promise((resolve, reject) => {
            const result = sqliteDB.queryData(sql, function(result){
                resolve(result)
            });
        })
    },
    count: function (sqliteDB) {
        const sql = `
            select count(*) from student
        `
        return new Promise((resolve, reject) => {
            const result = sqliteDB.queryData(sql, function(result){
                resolve(result)
            });
        })
    },
    query: function (sqliteDB, id) {
        const sql = `
            select * from student where id = ${id}
        `
        return new Promise((resolve, reject) => {
            const result = sqliteDB.queryData(sql, function(result){
                resolve(result)
            });
        })
    },
    del: function (sqliteDB, id) {
        const sql = `
            delete from student where id = ${id}
        `
        return new Promise((resolve, reject) => {
            const result = sqliteDB.executeSql(sql);
            resolve(result)
        })
    },
    end: null
}